item.js ➔ ???   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
cc 2
crap 2
rs 9.4285
1
import {warn, isNumericIDLike} from '../utils'
2
import BaseSyncer from './base'
3
4
/**
5
 * Item syncer used for when there's no constraints
6
 */
7
export default class ItemSyncer extends BaseSyncer {
8
9
	/**
10
	 * Handle new item creations from feathers
11
	 *
12
	 * @param item
13
	 */
14
	onItemCreated(item) {
15 2
		if (item[this._id] === this.filters.id) {
16 1
			this._set(item)
17
		}
18
	}
19
20
	/**
21
	 * Handle item updates from feathers
22
	 *
23
	 * @param item
24
	 */
25
	onItemUpdated(item) {
26 4
		if (item[this._id] === this.filters.id) {
27 2
			this._set(item)
28
		}
29
	}
30
31
	/**
32
	 * Handle item removals from feathers
33
	 *
34
	 * @param item
35
	 */
36
	onItemRemoved(item) {
37 2
		if (item[this._id] === this.filters.id) {
38 1
			this._remove()
39
		}
40
	}
41
42
	/**
43
	 * Bind watchers for computed values
44
	 *
45
	 * @private
46
	 */
47
	_bindComputedValues() {
48 15
		this.filters.id = null
49
50
		// When new value is found
51
		function callback(newVal) {
52 17
			this.filters.id = newVal
53
54
			// Warn about string id's that seem like they shooooouldn't
55
			/* istanbul ignore next */
56
			if (process.env.NODE_ENV !== 'production' && isNumericIDLike(newVal)) {
57
				warn('String ID that looks like a number given', this.path, newVal)
58
			}
59
60
			// Clear state (if now null it just makes sure)
61 17
			this.state = this._initialState()
62
63
			// Default return nothing
64 17
			let returning = false
65 17
			if (this.filters.id !== null) {
66 16
				returning = this._loadNewState()
67
			}
68
69 17
			if ('hook' in callback) {
70 15
				callback.hook(returning)
71 15
				delete callback.hook
72
			}
73
		}
74
75 15
		return new Promise(resolve => {
76 15
			callback.hook = resolve
77
78 15
			this.unwatchers.id = this.vm.$watch(this.settings.id, callback.bind(this), {immediate: true})
79
		})
80
	}
81
82
	/**
83
	 * Initial data for item syncer
84
	 *
85
	 * @returns {*}
86
	 * @private
87
	 */
88
	_initialState() {
89 48
		return null
90
	}
91
92
	/**
93
	 * Load the requested state
94
	 *
95
	 * @returns {Promise.<T>}
96
	 * @private
97
	 */
98
	_loadState() {
99 18
		return this.service.get(this.filters.id).then(item => {
100 15
			if (this.vm === null) {
101
				// Destroy has been called during loading
102 1
				return item
103
			}
104
105 14
			this._set(item)
106 14
			this._newStateLoaded()
107
108 14
			return item
109
		}).catch(this._handleStateLoadingError.bind(this))
110
	}
111
112
	/**
113
	 * Set current item
114
	 *
115
	 * @param item
116
	 * @private
117
	 */
118
	_set(item) {
119 17
		this.Vue.set(this, 'state', item)
120
	}
121
122
	/**
123
	 * Remove current item
124
	 *
125
	 * @private
126
	 */
127
	_remove() {
128 1
		this.state = this._initialState()
129
	}
130
}
131